home *** CD-ROM | disk | FTP | other *** search
/ Amiga Games Extra 1996 September / Amiga Games Extra CD-ROM 9-1996.iso / userbox / publicdomain / vim-4.2 / macros / maze / maze_mac < prev    next >
Text File  |  1996-05-29  |  12KB  |  272 lines

  1. " These macros 'solve' any maze produced by the a-maze-ing maze.c program.
  2. " First, a bit of maze theory.
  3. " If you were put into a maze, a guaranteed method of finding your way
  4. " out of the maze is to put your left hand onto a wall and just keep walking,
  5. " never taking your hand off the wall. This technique is only guaranteed to
  6. " work if the maze does not have any 'islands', or if the 'exit' is on the
  7. " same island as your starting point. These conditions hold for the mazes
  8. " under consideration.
  9. " Assuming that the maze is made up of horizontal and vertical walls spaced
  10. " one step apart and that you can move either north, south, east or west,
  11. " then you can automate this procedure by carrying out the following steps.
  12. " 1. Put yourself somewhere in the maze near a wall.
  13. " 2. Check if you have a wall on your left. If so, go to step 4.
  14. " 3. There is no wall on your left, so turn on the spot to your left and step
  15. "    forward by one step and repeat step 2.
  16. " 4. Check what is directly in front of you. If it is a wall, turn on the
  17. "    spot to your right by 90 degrees and repeat step 4.
  18. " 5. There is no wall in front of you, so step forward one step and
  19. "    go to step 2.
  20. " In this way you will cover all the corridors of the maze (until you get back
  21. " to where you started from, if you do not stop).
  22. " By examining a maze produced by the maze.c program you will see that 
  23. " each square of the maze is one character high and two characters wide.
  24. " To go north or south, you move by a one character step, but to move east or
  25. " west you move by a two character step. Also note that in any position
  26. " there are four places where walls could be put - to the north, to the south,
  27. " to the east and to the west.
  28. " A wall exists to the north of you if the character to the north of
  29. " you is a _ (otherwise it is a space).
  30. " A wall exists to the east of you if the character to the east of you
  31. " is a | (otherwise it is a .).
  32. " A wall exists to the west of you if the character to the west of you
  33. " is a | (otherwise it is a .).
  34. " A wall exists to the south of you if the character where you are
  35. " is a _ (otherwise it is a space).
  36. " Note the difference for direction south, where we must examine the character
  37. " where the cursor is rather than an adjacent cell.
  38. " If you were implementing the above procedure is a normal computer language
  39. " you could use a loop with if statements and continue statements, 
  40. " However, these constructs are not available in vi macros so I have used
  41. " a state machine with 8 states. Each state signifies the direction you
  42. " are going in and whether or not you have checked if there is a wall on
  43. " your left.
  44. " The transition from state to state and the actions taken on each transition
  45. " are given in the state table below.
  46. " The names of the states are N1, N2, S1, S2, E1, E2, W1, W2, where each letter
  47. " stands for a direction of the compass, the number 1 indicates that the we
  48. " have not yet checked to see if there is a wall on our left and the number 2
  49. " indicates that we have checked and there is a wall on our left.
  50. " For each state we must consider the existence or not of a wall in a
  51. " particular direction. This direction is given in the following table.
  52. " NextChar table:
  53. " state        direction       vi commands
  54. "  N1              W               hF
  55. "  N2              N               kF
  56. "  S1              E               lF
  57. "  S2              S               F
  58. "  E1              N               kF
  59. "  E2              E               lF
  60. "  W1              S               F
  61. "  W2              W               hF
  62. " where F is a macro which yanks the character under the cursor into
  63. " the NextChar register (n).
  64. " State table:
  65. " In the 'vi commands' column is given the actions to carry out when in
  66. " this state and the NextChar is as given. The commands k, j, ll, hh move
  67. " the current position north, south, east and west respectively. The
  68. " command mm is used as a no-op command.
  69. " In the 'next state' column is given the new state of the machine after
  70. " the action is carried out.
  71. " current state        NextChar    vi commands  next state
  72. "      N1                 .            hh          W1
  73. "      N1                 |            mm          N2
  74. "      N2                 _            mm          E1
  75. "      N2               space          k           N1
  76. "      S1                 .            ll          E1
  77. "      S1                 |            mm          S2
  78. "      S2                 _            mm          W1
  79. "      S2               space          j           S1
  80. "      E1               space          k           N1
  81. "      E1                 _            mm          E2
  82. "      E2                 |            mm          S1
  83. "      E2                 .            ll          E1
  84. "      W1               space          j           S1
  85. "      W1                 _            mm          W2
  86. "      W2                 |            mm          N1
  87. "      W2                 .            hh          W1
  88. "
  89. " Complaint about vi macros:
  90. " It seems that you cannot have more than one 'undo-able' vi command
  91. " in the one macro, so you have to make lots of little macros and
  92. " put them together.
  93. "
  94. " I'll explain what I mean by an example. Edit a file and
  95. " type ':map Q rXY'. This should map the Q key to 'replace the
  96. " character under the cursor with X and yank the line'.
  97. " But when I type Q, vi tells me 'Can't yank inside global/macro' and
  98. " goes into ex mode. However if I type ':map Q rXT' and ':map T Y',
  99. " everything is OK. I`m doing all this on a Sparcstation.
  100. " If anyone reading this has an answer to this problem, the author would
  101. " love to find out. Mail to gregm@otc.otca.oz.au.
  102. "
  103. " The macros:
  104. " The macro to run the maze solver is 'g'. This simply calls two other
  105. " macros: I, to initialise everything, and L, to loop forever running
  106. " through the state table.
  107. " Both of these macros are long sequences of calls to other macros. All
  108. " of these other macros are quite simple and so to understand how this
  109. " works, all you need to do is examine macros I and L and learn what they
  110. " do (a simple sequence of vi actions) and how L loops (by calling U, which
  111. " simply calls L again).
  112. "
  113. " Macro I sets up the state table and NextChar table at the end of the file.
  114. " Macro L then searches these tables to find out what actions to perform and
  115. " what state changes to make.
  116. "
  117. " The entries in the state table all begin with a key consisting of the
  118. " letter 's', the current state and the NextChar.  After this is the
  119. " action to take in this state and after this is the next state to change to.
  120. "
  121. " The entries in the NextChar table begin with a key consisting of the
  122. " letter 'n' and the current state. After this is the action to take to
  123. " obtain NextChar - the character that must be examined to change state.
  124. "
  125. " One way to see what each part of the macros is doing is to type in the
  126. " body of the macros I and L manually (instead of typing 'g') and see
  127. " what happens at each step.
  128. "
  129. " Good luck.
  130. "
  131. " Registers used by the macros:
  132. " s (State)        - holds the state the machine is in
  133. " c (Char)         - holds the character under the current position
  134. " m (Macro)        - holds a vi command string to be executed later
  135. " n (NextChar)     - holds the character we must examine to change state
  136. " r (Second Macro) - holds a second vi command string to be executed later
  137. "
  138. set remap
  139. set nomagic
  140. set noterse
  141. set wrapscan
  142. "
  143. "================================================================
  144. " g - go runs the whole show
  145. "        I - initialise
  146. "        L - then loop forever
  147. map g   IL
  148. "
  149. "================================================================
  150. " I - initialise everything before running the loop
  151. "   G$?.^M - find the last . in the maze
  152. "        ^ - replace it with an X (the goal)
  153. "   GYKeDP - print the state table and next char table at the end of the file
  154. "       0S - initialise the state of the machine to E1
  155. "      2Gl - move to the top left cell of the maze
  156. map I   G$?.^GYKeDP0S2Gl
  157. "
  158. "================================================================
  159. " L - the loop which is executed forever
  160. "        Q - save the current character in the Char register
  161. "        A - replace the current character with an 'O'
  162. "       ma - mark the current position with mark 'a'
  163. "      GNB - on bottom line, create a command to search the NextChar table
  164. "            for the current state
  165. " 0M0E@m^M - yank the command into the Macro register and execute it
  166. "       wX - we have now found the entry in the table, now yank the
  167. "            following word into the Macro register
  168. "     `a@m - go back to the current position and execute the macro, this will
  169. "            yank the NextChar in register n
  170. "   GT$B$R - on bottom line, create a command to search the state table
  171. "            for the current state and NextChar
  172. " 0M0E@m^M - yank the command into the Macro register and execute it
  173. "      2WS - we have now found the entry in the table, now yank the
  174. "            next state into the State macro
  175. "       bX - and yank the action corresponding to this state table entry
  176. "            into the Macro register
  177. "      GVJ - on bottom line, create a command to restore the current character
  178. "       0H - and save the command into the second Macro register
  179. "     `a@r - go back to the current position and exectute the macro to restore
  180. "            the current character
  181. "       @m - execute the action associated with this state
  182. "        U - and repeat
  183. map L   QAmaGNB0M0E@mwX`a@mGT$B$R0M0E@m2WSbXGVJ0H`a@r@mU
  184. "
  185. "================================================================
  186. " U - no tail recursion allowed in vi macros so cheat and set U = L
  187. map U   L
  188. "
  189. "================================================================
  190. " S - yank the next two characters into the State register
  191. map S   "sy2l
  192. "
  193. "================================================================
  194. " Q - save the current character in the Char register
  195. map Q   "cyl
  196. "
  197. "================================================================
  198. " A - replace the current character with an 'O'
  199. map A   rO
  200. "
  201. "================================================================
  202. " N - replace this line with the string 'n'
  203. map N   C/n
  204. "
  205. "================================================================
  206. " B - put the current state
  207. map B   "sp
  208. "
  209. "================================================================
  210. " M - yank this line into the Macro register
  211. map M   "my$
  212. "
  213. "================================================================
  214. " E - delete to the end of the line
  215. map E   d$
  216. "
  217. "================================================================
  218. " X - yank this word into the Macro register
  219. map X   "myt 
  220. "
  221. "================================================================
  222. " T - replace this line with the string 's'
  223. map T   C/s
  224. "
  225. "================================================================
  226. " R - put NextChar
  227. map R   "np
  228. "
  229. "================================================================
  230. " V - add the letter 'r' (the replace vi command)
  231. map V   ar
  232. "
  233. "================================================================
  234. " J - restore the current character
  235. map J   "cp
  236. "
  237. "================================================================
  238. " H - yank this line into the second Macro register
  239. map H   "ry$
  240. "
  241. "================================================================
  242. " F - yank NextChar (this macro is called from the Macro register)
  243. map F   "nyl
  244. "
  245. "================================================================
  246. " ^ - replace the current character with an 'X'
  247. map ^   rX
  248. "
  249. "================================================================
  250. " YKeDP - create the state table, NextChar table and initial state
  251. " Note that you have to escape the bar character, since it is special to
  252. " the map command (it indicates a new line).
  253. map Y   osE1  k  N1       sE1_ mm E2       sE2| mm S1       sE2. ll E1
  254. map K   osW1  j  S1       sW1_ mm W2       sW2| mm N1       sW2. hh W1
  255. map e   osN1. hh W1       sN1| mm N2       sN2  k  N1       sN2_ mm E1
  256. map D   osS1. ll E1       sS1| mm S2       sS2  j  S1       sS2_ mm W1
  257. map P   onE1 kF nE2 lF nW1 G$JF nW2 hF nN1 hF nN2 kF nS1 lF nS2 G$JF E1
  258.